package com.dwxt.information.service;

import com.dwxt.common.base.AdminUserType;
import com.dwxt.common.base.YesOrNo;
import com.dwxt.information.dao.InformationMapper;
import com.dwxt.information.entity.Information;
import com.dwxt.information.entity.InformationExample;
import com.dwxt.information.service.feign.AdminUserService;
import com.dwxt.information.websocket.WebSocketServer;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service
public class InformationService {

    @Autowired
    private InformationMapper informationMapper;

    @Autowired
    private AdminUserService adminUserService;


    public int count(String category) {
        String[] split = category.split(",");
        InformationExample example = new InformationExample();
        InformationExample.Criteria criteria = example.createCriteria();
        criteria.andCategoryEqualTo(split[0]).andIsReadEqualTo(YesOrNo.NO);
        if ("mall".equalsIgnoreCase(split[0])) {
            //是否是商家账户
            //BaseResult baseResult = adminUserService.selectUser();
            JSONObject user =
                    JSONObject.fromObject(adminUserService.selectUserById(Integer.parseInt(split[1])).getData());
            if (!user.getString("type").contains(AdminUserType.ADMIN)) {
                criteria.andSupplierIdEqualTo(user.getInt("id"));
            }
        }
        return ((Long) informationMapper.countByExample(example)).intValue();
    }

    public void insert(Information information) {
        informationMapper.insertSelective(information);
    }

    public void sendMsg(Information information) throws IOException {
        int count = count(information.getCategory() + "," + information.getSupplierId());
        JSONArray jsonArray = JSONArray.fromObject(adminUserService.selectAllAdminUser().getData());
        boolean flag = true;
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject user = jsonArray.getJSONObject(i);
            int id = user.getInt("id");
            WebSocketServer.sendInfo(count + "", information.getCategory() + id);
            if (information.getSupplierId() != null && id == information.getSupplierId()) {
                flag = false;
            }
        }
        if (flag) {
            WebSocketServer.sendInfo(count + "", information.getSupplierId() + "");
        }
    }

    public void close(Information information) {
        InformationExample example = new InformationExample();
        InformationExample.Criteria criteria = example.createCriteria();
        criteria.andCategoryEqualTo(information.getCategory());
        if ("mall".equalsIgnoreCase(information.getCategory())) {
            JSONObject user =
                    JSONObject.fromObject(adminUserService.selectUser().getData());
            if (!user.getString("type").contains(AdminUserType.ADMIN)) {
                criteria.andSupplierIdEqualTo(user.getInt("id"));
            }
        }
        information.setIsRead(YesOrNo.YES);
        informationMapper.updateByExampleSelective(information, example);
    }
}
